package dayOOP0715;

import java.util.Objects;

public class Car {
    //1.私有的成员变量
    private int doors;
    private int seats;
    private  String model;
    private  char   color;
    //2.构造方法//构造
    public Car() {
        this(null, '黑');
    }
    public Car(String model, char color) {
        this(0,0,model,color);
    }
    public Car(int doors, int seats, String model, char color) {
        this.doors = doors;
        this.seats = seats;
        this.model = model;
        this.color = color;
    }

    //3.get  set方法
    public int getDoors() {
        return doors;
    }

    public void setDoors(int doors) {
        this.doors = doors;
    }

    public int getSeats() {
        return seats;
    }

    public void setSeats(int seats) {
        this.seats = seats;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public char getColor() {
        return color;
    }

    public void setColor(char color) {
        this.color = color;
    }

    //4.hashCode
    //可以判断两个对象是否相同：
    //1.判断两个对象的hashCode是否相同，
    //  如果不相同则两个对象不同
    //  如果相同则再用equals比较两个对象的属性值(类似于身份证号)，如果相同则两个对象相同，否则不相同
    public int hashCode(){
        return Objects.hash(doors,seats,model,color);
    }

    //5.equals
    public boolean equals(Object other){
        //1.==比较this和参数other  如果相等则返回true
        if(this==other){return true;}
        //2.比较类型，如果other为null，则返回false，  如果不相等则返回false
        if( !(other instanceof  Car) ||  other ==null ){return false;}
        //3.比较属性，如果相等则返回true
        //如果比较的属性是引用类型，则用Objects的equals方法比较属性(否则，可能会导致空指针异常)
        Car  other2=(Car)other;
        return  this.seats==other2.seats  &&  this.doors==other2.doors  &&  Objects.equals(this.model,other2.model) && Objects.equals(this.color, other2.color);
    }



}
